/*
 * @Description:
 * @version:
 * @Author: sunqian_sr
 * @Date: 2023-03-03 11:09:50
 * @LastEditors: Andy
 * @LastEditTime: 2023-03-03 11:11:29
 */
import { defineStore } from '@/common/config/defineStore'
import { apiGetFindDictItem } from '../../api/common'
// import { defineStore } from 'pinia'
// 第一个参数是应用程序中 store 的唯一 id
export default defineStore('dictValue', {
  state: () => ({
    dictItemOptions: {
      name: '000000'
    }
  }),
  // 相当于 vue 中的 computed 计算属性 返回一个数值
  getters: {},
  actions: {
    getDictItem(dictKey) {
      return this.dictItemOptions[dictKey] || null
    },
    // 单独设置字典缓存值
    setDictItem(dictKey, dictArr) {
      this.dictItemOptions[dictKey] = dictArr
    },
    getSelectionDict(dictKey) {
      return new Promise((resolve, reject) => {
        ;(async () => {
          try {
            if (!this.dictItemOptions[dictKey]) {
              const { data } = await apiGetFindDictItem(dictKey)
              this.dictItemOptions[dictKey] = data
            }
            resolve(this.dictItemOptions[dictKey])
          } catch (error) {
            reject(error)
          }
        })()
      })
    }
  }
})
